package com.kaili.finance.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.response.CommonStatus;
import com.kaili.finance.mapper.AccountMapping;
import com.kaili.finance.pojo.dto.account.FinAccountUpdateDTO;
import com.kaili.finance.pojo.entity.FinAccountDetail;
import com.kaili.finance.pojo.entity.FinRecharge;
import com.kaili.finance.pojo.entity.FinTransferAcccounts;
import com.kaili.finance.pojo.vo.*;
import com.kaili.finance.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by lsw on 18-5-17.
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapping accountMapping;


    @Override
    public List<FinAccountInfoVO> queryAccountByLike(String accountName) {
        return accountMapping.queryAccountByLike(accountName);
    }

    @Override
    public Page<FinAccountDetailVO> queryAccountById(Page<FinAccountDetailVO> page, Long id) {
        List<FinAccountDetailVO> list = accountMapping.queryAccountById(page, id);
        page.setRecords(list);
        return page;
    }

    @Override
    @Transactional
    public CommonStatus addTranfer(FinTransferAcccounts fInAdjustVO) {
        Double balance = accountMapping.queryAccountBanlance(fInAdjustVO.getOutAccountId());
        //转账先判断转出账户余额够不够
        if(balance<fInAdjustVO.getAmount()){
            return CommonStatus.ACC_NO_BALANCE;
        }
        accountMapping.addTransfer(fInAdjustVO);
        accountMapping.finAdjustInput(String.valueOf(fInAdjustVO.getInputId()));
        return CommonStatus.ADD_OK;
    }

    @Override
    @Transactional
    public CommonStatus addRechargeWithdraw(FinRecharge finRechargeWithdrawVO) {
        Double balance = accountMapping.queryAccountBanlance(finRechargeWithdrawVO.getAccountId());
        //如果是提现先判断余额够不够
        if(balance<finRechargeWithdrawVO.getAmount() &&
                finRechargeWithdrawVO.getChargeItemId()==10){
            return CommonStatus.ACC_NO_BALANCE;
        }
        //插入充值/体现记录
        accountMapping.addRechargeWithdraw(finRechargeWithdrawVO);
        //调用存储过程来处理
        accountMapping.updateAccountBalance(String.valueOf(finRechargeWithdrawVO.getInputId()));
        return CommonStatus.ADD_OK;
    }

    @Override
    public Page<FinRechargeVO> queryRechargeWithdrawRecord(Integer type, Long accountId,
                                                                   Page<FinRechargeVO> page) {
        List<FinRechargeVO> list = accountMapping.queryRechargeWithdrawRecord(page,accountId, type);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<FinTransferAccountsDetailVO> queryAdjustRecord(Page<FinTransferAccountsDetailVO> page,Long inAccountId, Long outAccountId) {
        List<FinTransferAccountsDetailVO> list=accountMapping.queryAdjustRecord(page,inAccountId,outAccountId);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<FinSiteVO> querySiteNameByLike(String siteId, String siteName) {
        return accountMapping.querySiteNameByLike(siteId,siteName);
    }

    @Override
    public Long autoCreateAccountId() {
        return accountMapping.autoCreateAccountId();
    }

    @Override
    public CommonStatus addAccount(FinAccountDetail finAccountVO) {
        accountMapping.addAccount(finAccountVO);
        return CommonStatus.ADD_OK;
    }


    @Override
    public List<FinNewAccountInfoVO> queryAccountNameByLike(String accountName) {
        return accountMapping.queryAccountNameByLike(accountName);
    }

    @Override
    public FinLoginAccountVO queryAccountByLogin(Integer currentEmployeeId) {
        return accountMapping.queryAccountByLogin(currentEmployeeId);
    }

    @Transactional
    @Override
    public CommonStatus updateAccount(List<FinAccountUpdateDTO> accountVOList) {
        for(FinAccountUpdateDTO account:accountVOList){
            Integer count = accountMapping.updateAccount(account);
            if(count<=0){
                return CommonStatus.UPDATE_FAIL;
            }
        }
        return CommonStatus.UPDATE_OK;
    }
}
